package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.exception.BaseException;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用功能")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

/**
 * @description 文件上传功能
 * @author Administrator
 * @param[1] file
 * @throws
 * @return Result<String>
 * @time 2023/11/27 23:59
 */
    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) {
        String originalFilename = file.getOriginalFilename();
        String fileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url = null;
        try {
            url = aliOssUtil.upload(file.getBytes(), fileName);
        } catch (Exception ex) {
            throw new BaseException(MessageConstant.UPLOAD_FAILED);
        }
        return Result.success(url);

    }
}
